#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate scontrol update command for nodes is disabled.
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2002-2007 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set test_id        "26.1"
set exit_code      0
set node_list      ""
set node_name      ""

print_header $test_id

if {![test_alps]} {
        send_user "\nWARNING: This test is only compatible with Cray ALPS systems\n"
        exit $exit_code
}

#
# Identify a node and its state
#
spawn $sinfo --noheader -o  "NodeName=%N State=%t "
expect {
        -re "NodeName=($alpha_numeric_nodelist) State=idle " {
                if {[string compare $node_list ""] == 0} {
                        set node_list $expect_out(1,string)
                }
                exp_continue
        }
        -re "NodeName=($alpha_numeric_nodelist) State=allocated " {
                if {[string compare $node_list ""] == 0} {
                        set node_list $expect_out(1,string)
                }
                exp_continue
        }
	timeout {
		send_user "\nFAILURE: sinfo not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {[string compare $node_list ""] == 0} {
	send_user "\nWARNING: no nodes in usable state for this test\n"
	exit 0
}

#
# Convert node list to a single node name
#
log_user 0
spawn $scontrol show hostnames $node_list
expect {
        -re "($alpha_numeric_nodelist)" {
		set node_name $expect_out(1,string)
        }
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
log_user 1
if {[string compare $node_name ""] == 0} {
	send_user "\nWARNING: no nodes in usable state for this test\n"
	exit 0
}

#
# Change that node's state
#
set mod_error 0
spawn $scontrol update NodeName=$node_name State=DRAIN Reason=TESTING
expect {
	-re "can not be changed through Slurm" {
		set mod_error 1
		exp_continue
	}
	-re "slurm_update error: ($alpha_numeric_under) ($alpha_numeric_under)" {
		set access_err 0
		set err_msg1 $expect_out(1,string)
		set err_msg2 $expect_out(2,string)
		if {[string compare $err_msg1 "Invalid"] == 0} {
			set access_err 1
		}
		if {[string compare $err_msg2 "user"] == 0} {
			set access_err 1
		}
		if {$access_err == 1} {
			send_user "\nWARNING: user not authorized\n"
			exit $exit_code
		} else {
			set mod_error 1
		}
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$mod_error == 1} {
	send_user "\nThis error was expected, no worries\n"
} else {
	send_user "\nFAILURE: scontrol did not get an error trying to change node state\n"
	set exit_code 1
}

#
# Validate node's state
#
spawn $scontrol show node $node_name
expect {
        -re "($alpha_numeric_nodelist)" {
		set node_name $expect_out(1,string)
        }
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
}
exit $exit_code
